<?php
namespace app\api\controller;
use think\admin\Controller;
class Index extends Controller
{
    private $baidu_appid = "20240301001978474";
    private $baidu_ser = "1TqBkeyTjUELLC9LD4T1";
    private $baidu_translation_url = "https://fanyi-api.baidu.com/api/trans/vip/translate";

    public function index()
    {
        echo "Hello Word!";
    }

    public function translation()
    {
        $q = input('q');

        $salt = time() . rand(1000, 9999);
        $sign = md5($this->baidu_appid . $q . $salt . $this->baidu_ser);
        $data = [
            'q' =>urlencode($q),
            'from' => 'auto',
            'to'    => 'zh',
            'appid' => $this->baidu_appid,
            'salt'  => $salt,
            'sign'  => $sign
        ];
        $res = json_decode(http_get($this->baidu_translation_url, $data), true);
        if (array_key_exists('error_code',$res)) {
            $this->error("失败：".$res['error_msg']);
        } else {
            $this->success('成功', $res['trans_result']);
        }

    }
}